package com.q.tqb.util;

import android.os.Environment;
import android.text.TextUtils;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/12/26.
 */


public class StringUtil {

    public static final String SOUTH_PATH = Environment.getExternalStorageDirectory() + "/tqb/iat.wav";


    public static String getUpSound(int id) {
        return "sound/" + id + "_" + System.currentTimeMillis() + ".wav";
    }

    /**
     * 验证手机格式
     */
    public static boolean isMobile(String number) {
        //"[1]"代表第1位为数字1，"[34578]"代表第二位可以为3、4、5、7、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        String num = "[1][34578]\\d{9}";
        if (TextUtils.isEmpty(number)) {
            return false;
        } else {
            return number.matches(num);
        }
    }

    public static boolean validateIdNo(String no) {
        // 对身份证号进行长度等简单判断
        if (no == null || no.length() != 18 || !no.matches("\\d{17}[0-9X]")) {
            return false;
        }
        // 1-17位相乘因子数组
        int[] factor = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        // 18位随机码数组
        char[] random = "10X98765432".toCharArray();
        // 计算1-17位与相应因子乘积之和
        int total = 0;
        for (int i = 0; i < 17; i++) {
            total += Character.getNumericValue(no.charAt(i)) * factor[i];
        }
        // 判断随机码是否相等
        return random[total % 11] == no.charAt(17);
    }

    public static double halfUp(String money) {
        double result = Double.valueOf(money);
        BigDecimal b = new BigDecimal(result);
        result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return result;
    }


    public static String checkAccountMark(String result) {
        String regEx = "[^a-zA-Z0-9\\u4e00-\\u9fa5\\s,!:;?！；：。，？.]+$";
        Pattern pattern = Pattern.compile(regEx);
        Matcher m = pattern.matcher(result);
        LogUtil.i(result.replaceAll(regEx, ""));
        return m.replaceAll("");
    }

    public static String getToken(int uid) {
        return MD5Util.encrypt("znb" + MD5Util.encrypt("tqb" + uid));
    }

    public static String format(String s) {
        String str = s.replaceAll("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……& ;*（）——+|{}【】‘；：”“’。，、？|-]", "").replaceAll(" ", "");
        return str;
    }


    public static boolean isMate(String input, String right) {
        right = format(right);
        LogUtil.i("right" + right);
        if (input.length() != right.length()) {
            return false;
        }
        if (input.equals(right)) {
            return true;
        } else {
            try {
                input = PinyinHelper.convertToPinyinString(input, ";", PinyinFormat.WITHOUT_TONE);
                right = PinyinHelper.convertToPinyinString(right, ";", PinyinFormat.WITHOUT_TONE);
                if (input.equals(right)) {
                    return true;
                } else {
                    String[] inputs = input.split(";");
                    String[] rights = right.split(";");
                    int size = rights.length;
                    int amount = 0;
                    for (int i = 0; i < size; i++) {
                        if (inputs[i].equalsIgnoreCase(rights[i])) {
                            amount++;
                        }
                    }
                    //     LogUtil.i(amount * 100 / size + "saaaaaaa");
                    return amount * 100 / size >= 75;

                }
            } catch (PinyinException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static String formatCard(String cardNo) {
        if (TextUtils.isEmpty(cardNo) || cardNo.length() < 8) {
            return "卡号有误";
        }
        // int size =
        String card = "";
        // card = cardNo.substring(0, 4) + " **** **** **** *** ";
        card = " **** **** **** ***" + cardNo.substring(cardNo.length() - 4, cardNo.length() - 3) + " " + cardNo.substring(cardNo.length() - 3);
        return card;
    }
}
